// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: qiang

#include "coin2/exchange/apollox/api_order/native_rest_async_asio_subscriber.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

#include "coin2/base/log.h"

namespace coin2::exchange::apollox::api_order {

void ApolloxRestAsyncAsioSubscriber::QueryBalance() {
  RestParams params;
  params.set_recv_window(5000);
  auto http_context = rest_client()->QueryAccountBalance(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&ApolloxRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void ApolloxRestAsyncAsioSubscriber::QueryOpenOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_recv_window(5000);
  auto http_context = rest_client()->QueryOpenOrders(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&ApolloxRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void ApolloxRestAsyncAsioSubscriber::QueryHistoryOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_limit(90);
  params.set_recv_window(5000);
  auto http_context = rest_client()->QueryAllOrders(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&ApolloxRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void ApolloxRestAsyncAsioSubscriber::QueryFills(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_limit(90);
  params.set_recv_window(5000);
  auto http_context = rest_client()->QueryTrades(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&ApolloxRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void ApolloxRestAsyncAsioSubscriber::GenListenKey() {
  auto http_context = rest_client()->GenListenKey();
  if (!http_context) return;
  http_context->handler =
      std::bind(&ApolloxRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void ApolloxRestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(
      std::bind(&ApolloxRestAsyncAsioSubscriber::GenListenKey, this),
      300,
      "gen_listen_key");

  AddPeriodicTask(
      std::bind(&ApolloxRestAsyncAsioSubscriber::QueryBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance");

  const auto symbol_list = native_symbols();

  if (symbol_list.size() < 14) {
    AddPeriodicTask(
        std::bind(&ApolloxRestAsyncAsioSubscriber::QueryOpenOrders, this, std::placeholders::_1),
        config_.query_open_orders_period_sec(),
        "query_open_orders",
        symbol_list);
  } else {
    if (config_.query_open_orders_period_sec() < 60) {
      LOG(WARNING) << "query open orders is too frequent as it consumes 40 rate limit per time";
    }
    AddPeriodicTask(
        std::bind(&ApolloxRestAsyncAsioSubscriber::QueryOpenOrders, this, ""),
        config_.query_open_orders_period_sec(),
        "query_open_orders");
  }

  AddPeriodicTask(
      std::bind(&ApolloxRestAsyncAsioSubscriber::QueryHistoryOrders, this, std::placeholders::_1),
      config_.query_cancel_orders_period_sec(),
      "query_history_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&ApolloxRestAsyncAsioSubscriber::QueryFills, this, std::placeholders::_1),
      config_.query_fill_period_sec(),
      "query_fills",
      symbol_list);
}

}  // namespace coin2::exchange::apollox::api_order
